<template>
  <div class="login-container">
    <el-form ref="loginForm" class="login-form" label-width="100px" :model="form" :rules="rules">
      <div class="title-container">
        <img src="/logo.png" alt="">
      </div>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item prop="captcha" label="验证码" class="captcha-container">
        <div class="captcha">
          <img :src="code.captcha" alt="" @click="updateCaptcha">
        </div>
        <el-input v-model="form.captcha" class="w300" placeholder="请输入验证码" />
      </el-form-item>
      <el-form-item prop="emailcode" label="邮件验证码" class="captcha-container">
        <div class="captcha">
          <el-button :disable="send.timer > 0" type="primary" @click="sendEmailCode">
            {{ sendText }}
          </el-button>
        </div>
        <el-input v-model="form.emailcode" class="w300" placeholder="请输入邮件验证码" />
      </el-form-item>
      <el-form-item prop="passwd" label="密码">
        <el-input v-model="form.passwd" type="password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="onSubmit">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import md5 from 'md5'
export default {
  layout: 'login',
  data () {
    return {
      code: {
        captcha: '/api/captcha?+t=' + new Date().getTime()
      },
      form: {
        email: '2535924382@qq.com',
        captcha: '',
        nickname: '小白',
        passwd: '123456',
        emailcode: ''
      },
      send: {
        timer: 0
      },
      rules: {
        email: [
          { required: true, message: '请输入电子邮箱', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'change' }
        ],
        emailcode: [
          { required: true, message: '请输入邮件验证码', trigger: 'change' }
        ],
        passwd: [
          { required: true, pattern: /^[\w_-]{6,12}$/g, message: '请输入6~12位密码', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    sendText () {
      if (this.send.timer <= 0) {
        return '发送'
      }
      return `${this.send.timer}s后发送`
    }
  },
  beforeDestroy () {
    clearInterval(this.timer)
  },
  methods: {
    onSubmit () {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          const obj = {
            email: this.form.email,
            captcha: this.form.captcha,
            emailcode: this.form.emailcode,
            passwd: md5(this.form.passwd)
          }
          const ret = await this.$http.post('/user/login', obj)
          if (ret.code === 0) {
            // token的存储，登录成功，返回token
            this.$message.success('登录成功')
            localStorage.setItem('token', ret.data.token)
            setTimeout(() => {
              this.$router.push('/')
            }, 500)
          } else {
            this.$message.error(ret.message)
          }
        }
      })
    },
    updateCaptcha () {
      this.code.captcha = '/api/captcha?+t=' + new Date().getTime()
    },
    async sendEmailCode () {
      // @todo
      await this.$http.get('/sendcode?email=' + this.form.email)
      this.send.timer = 10
      this.timer = setInterval(() => {
        this.send.timer -= 1
        if (this.send.timer === 0) {
          clearInterval(this.timer)
        }
      }, 1000)
    }
  }
}
</script>

<style scoped lang="stylus">
.form-style {
  width: 800px;
  margin: 0 auto;
}
</style>
